package com.be.beadmin.project.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.project.dto.input.BatchPlanDto;
import com.be.beadmin.project.dto.input.BatchPlanInfoFormDto;
import com.be.beadmin.project.dto.input.InfoAndPage;
import com.be.beadmin.project.dto.input.PlanStageDto;
import com.be.beadmin.project.dto.output.BatchPlanEx;
import com.be.beadmin.project.dto.output.BatchPlanInfoFormEx;
import com.be.beadmin.project.service.BatchPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author ljh
* @date 2023-02-05
**/
@Api(tags = {"项目管理-批次计划"})
@RestController
public class BatchPlanController {

    @Autowired
    private  BatchPlanService batchPlanService;


    @ApiOperation("获取批次计划分页信息")
    @PostMapping(value = "/project/getBatchPlanByPage")
    public ResultModel getBatchPlanByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(batchPlanService.getBatchPlanByPage(pageEntity)) ;
    }

//    @ApiOperation("获取批次计划的简单分页信息，生产排程中需要用到")
//    @PostMapping(value = "/project/getBatchPlanInProductByPage")
//    public ResultModel getBatchPlanInProductByPage(@RequestBody PageEntity pageEntity)  {
//        return ResultModel.success(batchPlanService.getBatchPlanInProductByPage(pageEntity)) ;
//    }

    @ApiOperation("获取所有批次计划及各阶段分页信息")
    @PostMapping(value = "/project/getBatchPlanAndStageByPage")
    public ResultModel getBatchPlanAndStageByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(batchPlanService.getBatchPlanAndStageByPage(pageEntity)) ;
    }

    @ApiOperation("获取未分配柜体的数量")
    @PostMapping(value = "/project/countUnAssginProduct")
    public ResultModel countUnAssginProduct(@RequestParam String infoId )  {
        return ResultModel.success(batchPlanService.countUnAssginProduct(infoId)) ;
    }
    @ApiOperation("根据项目Id获取批次计划及各阶段分页信息")
    @PostMapping(value = "/project/getBatchPlanAndStageByPageAndInfoId")
    public ResultModel getBatchPlanAndStageByPageAndInfoId(@RequestBody InfoAndPage infoAndPage)  {
        return ResultModel.success(batchPlanService.getBatchPlanAndStageByPageAndInfoId(infoAndPage)) ;
    }
    @ApiOperation("根据项目Id获取批次计划分页信息")
    @PostMapping(value = "/project/getBatchPlanByPageAndInfoId")
    public ResultModel getBatchPlanByPageAndInfoId(@RequestBody InfoAndPage infoAndPage)  {
        return ResultModel.success(batchPlanService.getBatchPlanByPageAndInfoId(infoAndPage)) ;
    }

    @ApiOperation("生成添加批次计划的表单项")
    @PostMapping("/project/genAddBatchPlanForm")
    public ResultModel genAddBatchPlanForm(@RequestParam String infoId){
//        batchPlanService.addBatchPlan(batchPlanDto);
//        return ResultModel.success(ResultModel.ADD);
        BatchPlanInfoFormEx result = batchPlanService.genAddBatchPlanForm(infoId);

        if (result == null){
//            BatchPlanInfoFormEx[] result1 = new BatchPlanInfoFormEx[0];
            return ResultModel.success(null);
        }
//        if (result == -1){
//            System.out.println("yeah:"+result);
//            return ResultModel.error(-1,"该项目已有该批次计划，请勿重复添加");
//        }
        return ResultModel.success(result);

    }


    @ApiOperation("修改一个批次计划各阶段时间")
    @PostMapping("/project/updateBatchPlanStageTime")
    public ResultModel updateBatchPlanStageTime(@RequestBody List<PlanStageDto> planStageDtos){
//        try {
        batchPlanService.updateBatchPlanStageTime(planStageDtos);
//        } catch (Exception e) {
//            System.out.println(e.getCause());
//            return ResultModel.error(-1,e.getMessage());
//        }
        return ResultModel.success(ResultModel.UPDATE);

    }

    @ApiOperation("修改一个项目批次计划的基础时间")
    @PostMapping("/project/updateBatchPlanTime")
    public ResultModel updateBatchPlanTime(@RequestBody BatchPlanDto batchPlanDto){
        batchPlanService.updateBatchPlanTime(batchPlanDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("修改一个项目批次计划的柜体信息")
    @PostMapping("/project/updateBatchPlanProduct")
    public ResultModel updateBatchPlanProduct(@RequestBody BatchPlanInfoFormDto batchPlanDto){

        int result = batchPlanService.updateBatchPlanProduct(batchPlanDto);
        return ResultModel.success(result);
    }

    @ApiOperation("生成修改批次计划的表单项")
    @PostMapping("/project/genUpdateBatchPlanForm")
    public ResultModel genUpdateBatchPlanForm(@RequestParam String batchPlanId){
//        batchPlanService.addBatchPlan(batchPlanDto);
//        return ResultModel.success(ResultModel.ADD);
        BatchPlanInfoFormEx result = batchPlanService.genUpdateBatchPlanForm(batchPlanId);

//        if (result == -1){
//            System.out.println("yeah:"+result);
//            return ResultModel.error(-1,"该项目已有该批次计划，请勿重复添加");
//        }
        if (result==null){

            return ResultModel.error(-1,"没有查找到批次计划");
        }
        return ResultModel.success(result);

    }


    @ApiOperation("导出一个项目批次计划")
    @PostMapping("/project/exportBatchPlan")
    public void exportBatchPlan(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<BatchPlanEx> batchPlanPage = batchPlanService.getBatchPlanByPage(pageEntity);
        List<BatchPlanEx> batchPlanExes = batchPlanPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("BatchPlan接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), BatchPlanEx.class).sheet("sheet1").doWrite(batchPlanExes);
    }

    @ApiOperation("获取批次号")
    @PostMapping("/project/getBatchPlanCode")
    public ResultModel getBatchPlanCode(){
        return ResultModel.success(batchPlanService.getBatchPlanCode()) ;
    }

}